use serde::{Deserialize, Serialize};
use serde_yaml;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

#[derive(Serialize, Deserialize)]
pub struct ProjectConfig {
    pub path: PathBuf,
    pub handle_ext: Vec<String>,
    #[serde(default)]
    pub exclude_folders: Option<Vec<String>>,
}

impl ProjectConfig {
    pub fn load() -> Option<Self> {
        let mut file = match File::open("config.yml") {
            Ok(file) => file,
            Err(_) => return None,
        };

        let mut contents = String::new();
        if file.read_to_string(&mut contents).is_err() {
            return None;
        }

        match serde_yaml::from_str(&contents) {
            Ok(config) => Some(config),
            Err(_) => None,
        }
    }
}
